<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="简称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="simpleName">
              <a-input v-model="model.simpleName" placeholder="请输入简称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contact">
              <a-input v-model="model.contact" placeholder="请输入联系人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="telephone">
              <a-input v-model="model.telephone" placeholder="请输入联系电话"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="客户分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cl">
              <j-dict-select-tag type="list" v-model="model.cl" dictCode="erp_customer_cl,name,id"
                                 placeholder="请选择客户分类"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="客户类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="erp_customer_type,name,id"
                                 placeholder="请选择客户类型"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="客户类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="category">
              <j-dict-select-tag type="list" v-model="model.category" dictCode="erp_customer_category,name,id"
                                 placeholder="请选择客户类别"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="品牌" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="brand">
              <j-dict-select-tag type="list" v-model="model.brand" dictCode="erp_brand,name,id" placeholder="请选择品牌"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="国家" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="countryId">
              <j-dict-select-tag type="list" v-model="model.countryId" dictCode="country" placeholder="请选择国家"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="开发时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deDate">
              <j-date placeholder="请选择开发时间" v-model="model.deDate" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="业务员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="salesman">
              <j-select-user-by-dep-v2 v-model="model.salesman" :multi="true"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="所属洲" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="continent">
              <a-input v-model="model.continent" placeholder="请输入所属洲"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="销售区域" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="salesArea">
              <a-input v-model="model.salesArea" placeholder="请输入销售区域"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
              <a-input v-model="model.address" placeholder="请输入地址"></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="email">
              <a-input v-model="model.email" placeholder="请输入邮箱"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status" dictCode="erp_status" placeholder="请选择状态"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="textareaLabelCol" :wrapperCol="textareaWrapperCol" prop="remark">
              <a-input type="textarea" v-model="model.remark" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import {httpAction, getAction} from '@/api/manage'
import {validateDuplicateValue} from '@/utils/util'

export default {
  name: 'ErpCustomerForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        status: 1
      },
      labelCol: {
        xs: {span: 24},
        sm: {span: 5},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },
      textareaLabelCol: {
        xs: {span: 12},
        sm: {span: 2},
      },
      textareaWrapperCol: {
        xs: {span: 40},
        sm: {span: 21},
      },
      confirmLoading: false,
      validatorRules: {
        name: [
          {required: true, message: '请输入名称!'},
        ],
        simpleName: [
          {required: true, message: '请输入简称!'},
        ],
        contact: [
          {required: true, message: '请输入联系人!'},
        ],
        telephone: [
          {required: true, message: '请输入联系电话!'},
        ],
        status: [
          {required: true, message: '请输入状态!'},
        ],
        cl: [
          {required: true, message: '请输入客户分类!'},
        ],
        type: [
          {required: true, message: '请输入客户类型!'},
        ],
        category: [
          {required: true, message: '请输入客户类别!'},
        ],
        brand: [
          {required: true, message: '请输入品牌!'},
        ],
        countryId: [
          {required: true, message: '请输入国家!'},
        ],
        deDate: [
          {required: true, message: '请输入开发时间!'},
        ],
        salesman: [
          {required: true, message: '请输入业务员!'},
        ],
        email: [
          {required: false},
          {pattern: /^([\w]+\.*)([\w]+)@[\w]+\.\w{3}(\.\w{2}|)$/, message: '请输入正确的电子邮件!'},
        ],
      },
      url: {
        add: "/base/erpCustomer/add",
        edit: "/base/erpCustomer/edit",
        queryById: "/base/erpCustomer/queryById"
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>